package cn.com.callback

import cn.com.kh.KHDBConfig
import groovy.json.*;
import groovy.swing.impl.DefaultAction
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import cn.com.callback.CallBackInvoke
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import org.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.kh.getProperty;
import cn.com.kh.KHUtil;

import org.json.JSONArray
import org.json.JSONException;

/**
 *
 * @Title: ApiBPLogin* @Description:深圳宝安排水小程序登录接口--外部接口
 * @author: chieh* @date 2020-11-08 21:28:00
 *
 */
public class ApiBPLogin implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(ApiBPLogin.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        log.debug("-------------------------start ApiBPLogin 开始-----------------");

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());
        String reJson = null;
        long endTime = 0;

        //获取参数js_code，登录时获取的 code
        def js_code = request.getParameter("js_code");
        log.debug("js_code" + js_code);

        JSONArray resArray = new JSONArray();
        JSONObject resObject = new JSONObject();
        try {
            if (request.getParameter("js_code") == null) {
                endTime = System.currentTimeMillis();
                reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"处理失败(参数输入异常),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
                return reJson;
            }
            String getParam = "{\"code\":\"" + js_code + "\",\"opsource\":\"WX1\"}";
            String getResult = Util.doHttpPost(KHDBConfig.getAPIURL(eliteBean, "EliteInner-API") + "/apiWXMiniPCode", getParam, "UTF-8", 100000, 0);
            log.debug("getResult调用结果返回：" + getResult.toString());
            if (getResult != null && getResult.length() > 0) {
                try {
                    JSONObject loginObject = new JSONObject(getResult);
                    String openid = KHUtil.GetJV(loginObject, "openid");
                    String session_key = KHUtil.GetJV(loginObject, "login_key");
                    String unionid = KHUtil.GetJV(loginObject, "unionid");
                    String errcode = KHUtil.GetJV(loginObject, "errcode");
                    String errmsg = KHUtil.GetJV(loginObject, "errmsg");

                    //返回值
                    resObject.put("openid", openid);
                    resObject.put("session_key", session_key);
                    resObject.put("unionid", unionid);
                    resObject.put("errcode", errcode);
                    resObject.put("errmsg", errmsg);
                    resArray.put(resObject);
                } catch (Exception e) {
                    resArray = null;
                }
            }
            endTime = System.currentTimeMillis();
            reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"获取的登录接口调用处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":" + resArray + "}";

        } catch (Exception e) {
            log.error("接口处理异常" + e.getMessage());
            // 失败返回
            reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"获取的登录接口调用处理失败,请检查确认入参有效且合法\",\"data\":[]}";

        }
        return reJson;

    }
}